#ifndef DBFILE_H
#define DBFILE_H

#include "TwoWayList.h"
#include "Record.h"
#include "Schema.h"
#include "File.h"
#include "Comparison.h"
#include "ComparisonEngine.h"

typedef enum {heap, sorted, tree} fType;

// Do NOT introduce pointer in the structure - metaData. If you have to, then change
// the function FileMetaData::WriteMetaData.
typedef struct metaData{
	fType myType;
};

class FileMetadata{
private:
	metaData mydata;
	void GenerateMetadataName(char* f_path,char** f_metadata);
public:
	FileMetadata();
	int LoadMetaData(char* f_path);
	void SetMetaData(fType type);
	int WriteMetaData(char* f_path);
};

class DBFile {
private:
	Page *myPage;
	File *myFile;
	Record *currentRecord;
	off_t currentPage; //Page number starts from 0
	off_t lastReadPage;
	long lastReadRecord; //Record number starts from 1
	FileMetadata myMetadata;
	bool isDirty;
	bool isAddDone;
	int writeDirtyPage();

public:
	DBFile (); 
	~DBFile();
	int Create (char *fpath, fType file_type, void *startup);
	int Open (char *fpath);
	int Close ();
	void Load (Schema &myschema, char *loadpath);
	void MoveFirst ();
	void Add (Record &addme);
	int GetNext (Record &fetchme);
	int GetNext (Record &fetchme, CNF &cnf, Record &literal);

};
#endif
